package com.aree.caipiao.shsq.chain;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.aree.caipiao.shsq.model.ShuangSeQiuNumbers;

/**
 * 随机筛选过滤器
 * @author hp-pc
 *
 */
public class RandomChooseChain extends ChooseChain {
	private static Random random = new Random();
	private int numbers;//保留的个数
	
	public RandomChooseChain(int numbers) {
		this.numbers = numbers;
	}
	
	public List<Integer> filter(List<Integer> allNumbersIndexes) {
		log.debug(this.getClass().getName() + "开始过滤");
		if (isEmpty(allNumbersIndexes)) return allNumbersIndexes;
		List<Integer> results = new ArrayList<Integer>();
		if (this.numbers <= 0) return results;
		for (int i = 0; i < this.numbers; i++) {
			int index = random.nextInt(allNumbersIndexes.size());
			if (results.contains(index)) {
				i--;
			} else {
				results.add(index);
			}
		}
		log.debug(this.getClass().getName() + "完成过滤");
		if (getNext() != null) results = getNext().filter(results);
		return results;
	}
	
	/* (non-Javadoc)
	 * @see com.aree.caipiao.shsq.chain.ChooseChain#isWanted(com.aree.caipiao.shsq.model.ShuangSeQiuNumbers)
	 */
	@Override
	protected boolean isWanted(ShuangSeQiuNumbers numbers) {
		// 在此类中没有用处
		return true;
	}
	
	public static void main(String[] args) {
		int t = 1000;
		for (int i = 0; i < t*2; i++) {
			int n = random.nextInt(t);
			if (n < 10) System.out.print("00" + n+",");
			else if (n < 100) System.out.print("0" + n +",");
			else System.out.print(n+",");
			if (i % 20 == 0) System.out.println();
		}
	}
}
